package com.estronger.shopmanage.ui.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.MainListAdapter;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.bean.B8Wether;
import com.estronger.shopmanage.bean.B9Share;
import com.estronger.shopmanage.bean.MenuPermission;
import com.estronger.shopmanage.ui.home.presenter.IMainView;
import com.estronger.shopmanage.ui.home.presenter.MainPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.soonfor.evaluate.bean.EvaluateRankingBean;
import com.soonfor.evaluate.tools.SwipeRefreshHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShareArticleActivity extends BaseActivity implements IMainView {

    @BindView(R.id.swipe_refresh)
    protected SmartRefreshLayout mSwipeRefresh;
    @BindView(R.id.llftxterror)
    protected LinearLayout llfNoData; //错误的信息
    @BindView(R.id.llfdata)
    protected LinearLayout llfdata; //有数据时
    @BindView(R.id.txterror)
    protected TextView tvfMsg;
    @BindView(R.id.recyView)
    RecyclerView mRecyArticle;
    private MainListAdapter adapter;
    private MainPresenter presenter;
    private int currpageNo = 1;
    private ArrayList<B9Share> arrayList;

    private void onBackViewClick(){
        finish();
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharearticle);
        ButterKnife.bind(this);
        presenter = new MainPresenter(this);
        initSwipeRefresh();
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        mRecyArticle.setLayoutManager(manager);
        adapter = new MainListAdapter(this);
        mRecyArticle.setAdapter(adapter);
        try {
            arrayList = getIntent().getParcelableArrayListExtra("SHARELIST");
        }catch (Exception e){}
        mSwipeRefresh.setEnableLoadmore(false);
        mSwipeRefresh.autoRefresh();
    }


    @Override
    public void refeshGrildView(int requestCode, boolean isSuccess, MenuPermission mpRight) {

    }

    @Override
    public void refreshWeather(B8Wether weather) {}

    @Override
    public void showArticles(ArrayList<B9Share> dataList, boolean isSuccess) {
        mSwipeRefresh.finishRefresh();
        if(mRecyArticle!=null && isSuccess){
            llfNoData.setVisibility(View.GONE);
            llfdata.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged(dataList);
        }else {
            if (arrayList!=null && arrayList.size()>0){
                llfNoData.setVisibility(View.GONE);
                llfdata.setVisibility(View.VISIBLE);
                adapter.notifyDataSetChanged(arrayList);
            }else {
                llfdata.setVisibility(View.GONE);
                llfNoData.setVisibility(View.VISIBLE);
                tvfMsg.setText("暂无数据");
            }
        }
    }

    @Override
    public void showNoEvalCustomerNum(int stnum) {
    }
    @Override
    public void setEvaluateInfoToView(List<EvaluateRankingBean> rankingBeanList, int ranking) {

    }

    /**
     * 初始化下拉刷新
     */
    private void initSwipeRefresh() {
        if (mSwipeRefresh != null) {
            //设置 Header 为 Material风格
            // mSwipeRefresh.setRefreshHeader(new MaterialHeader(getBaseContext()).setShowBezierWave(false).setColorSchemeColors(getResources().getColor(R.color.blue)));
            SwipeRefreshHelper.init(mSwipeRefresh, new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    presenter.getShareArticles(ShareArticleActivity.this);
                }
            });
//            mSwipeRefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
//                @Override
//                public void onLoadmore(RefreshLayout refreshlayout) {
//                    presenter.getShareArticles(ShareArticleActivity.this);
//                }
//            });
        }
    }
}
